package com.mycc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mycc.common.Code;
import com.mycc.common.Result;
import com.mycc.damin.TbGuides;
import com.mycc.service.GuidesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GuidesController {
    @Autowired
    private GuidesService guideService;

    //添加
    @PostMapping("/addGuide")
    public Result addGuide(@RequestBody TbGuides guide) {
        boolean flag = guideService.addGuides(guide);
        if (flag) {
            return new Result(Code.ADD_OK, "ok", flag);
        } else {
            return new Result(Code.ADD_FAIL, "error", flag);
        }
    }

    //删除
    @DeleteMapping("/deleteGuide/{id}")
    public Result deleteGuide(@PathVariable("id") int id) {
        boolean flag = guideService.deleteGuides(id);
        if (flag) {
            return new Result(Code.DELETE_OK, "ok", flag);
        } else {
            return new Result(Code.DELETE_FAIL, "error", flag);
        }
    }


    //查询全部导游
    @GetMapping("/selAll/{pageNumber}/{pageSize}")
    public IPage<TbGuides> selPage(@PathVariable Integer pageNumber, @PathVariable Integer pageSize){
        return  guideService.selAllGuides(pageNumber, pageSize);
    }

    //通过id查询
    @GetMapping("/selByIdGuide/{id}")
    public Result selById(@PathVariable("id") int id) {
        TbGuides guide = guideService.selByIdGuides(id);
        if (guide != null) {
            return new Result(Code.SELECT_OK, "ok", guide);
        } else {
            return new Result(Code.SELECT_FAIL, "error", guide);
        }
    }


    //通过名字查询
    @GetMapping("/selByName/{name}")
    public Result selByName(@PathVariable("name") String name) {
        List<TbGuides> list = guideService.selByName(name);
        if (list != null) {
            return new Result(Code.SELECT_OK, "ok", list);
        } else {
            return new Result(Code.SELECT_FAIL, "error", list);
        }
    }

    //通过地区查询
    @GetMapping("/selByLocation/{location}")
    public Result selByLocation(@PathVariable("location") String location) {
        List<TbGuides> list = guideService.selByLocation(location);
        if (list != null) {
            return new Result(Code.SELECT_OK, "ok", list);
        } else {
            return new Result(Code.SELECT_FAIL, "error", list);
        }
    }

    //通过id进行修改
    @PutMapping("/updateGuide/{id}")
    public Result updateGuide(@PathVariable("id") int id, @RequestBody TbGuides guide) {
        boolean flag = guideService.updateGuides(id,guide);
        if (flag) {
            return new Result(Code.SET_OK, "ok", flag);
        } else {
            return new Result(Code.SET_FAIL, "error", flag);
        }
    }
}

